package com.wzl.customviewproject.util;

import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：longxiansen
 * 功能：
 * STEP1:实现最基本的一个流式布局展示，做到多个标签超出一行则显示到下一行。核心就是childView.measure()方法、setMeasuredDimension()方法、childView.layout()方法
 */
public class MyFlowLayout extends ViewGroup {
    public MyFlowLayout(Context context) {
        super(context);
    }

    public MyFlowLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyFlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private static int mHorizontalSpacing = dp2px(16);
    private static int mVerticalSpacing = dp2px(8);

    private static int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, Resources.getSystem().getDisplayMetrics());
    }
    private List<List<View>> allLines;
    private List<Integer> lineHeights;

    private void initMeasure() {
        allLines = new ArrayList<>();
        lineHeights = new ArrayList<>();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        initMeasure();
        int childCount = getChildCount();

        int selfWidth = MeasureSpec.getSize(widthMeasureSpec);
        int selfHeight = MeasureSpec.getSize(heightMeasureSpec);


        int parentNeededWidth = 0;
        int parentNeededHeight = 0;


        int lineUsedWidth = 0;//一行已使用的宽度
        List<View> lineViews = new ArrayList<>();
        int lineHeight = 0;//行高


        for (int i = 0; i < childCount; i++) {
            View childView = getChildAt(i);

            LayoutParams childViewLayoutParams = childView.getLayoutParams();
            //为什么这么传paddingLeft + paddingRight
            int childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec, getPaddingLeft() + getPaddingRight(), childViewLayoutParams.width);
            int childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec, getPaddingTop() + getPaddingBottom(), childViewLayoutParams.height);
            childView.measure(childWidthMeasureSpec,childHeightMeasureSpec);

            //判断一下
            int measuredWidth = childView.getMeasuredWidth();
            int measuredHeight = childView.getMeasuredHeight();


            if (measuredWidth + mHorizontalSpacing +  lineUsedWidth > selfWidth) {
                //超出一行了
                allLines.add(lineViews);
                lineHeights.add(lineHeight);

                //parentNeededWidth是所有行的最大值
                // 和parentNeededHeight是所有的行的总和
                parentNeededWidth = Math.max(parentNeededWidth,lineUsedWidth+mHorizontalSpacing);
                parentNeededHeight = parentNeededHeight + lineHeight + mVerticalSpacing;

                //重置
                lineViews = new ArrayList<>();
                lineUsedWidth = 0;
                lineHeight = 0;
            }

            lineViews.add(childView);
            lineHeight = Math.max(lineHeight,measuredHeight);
            lineUsedWidth = lineUsedWidth + mHorizontalSpacing + measuredWidth;

            if (i == childCount-1){
                allLines.add(lineViews);
                lineHeights.add(lineHeight);
            }
        }


        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int realWidth = (widthMode == MeasureSpec.EXACTLY)?selfWidth:parentNeededWidth;
        int realHeight = (heightMode == MeasureSpec.EXACTLY)?selfHeight : parentNeededHeight;

        setMeasuredDimension(realWidth,realHeight);
    }



    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int curL = getPaddingLeft();
        int curT = getPaddingTop();

        for (int i = 0; i < allLines.size(); i++) {
            List<View> lineViews = allLines.get(i);
            Integer lineHeight = lineHeights.get(i);


            for (int j = 0; j < lineViews.size(); j++) {
                View childView = lineViews.get(j);

                int left = curL;
                int measuredWidth = childView.getMeasuredWidth();
                int right = left +  measuredWidth;
                int top = curT;
                int measuredHeight = childView.getMeasuredHeight();
                int botttom = top + measuredHeight;
                childView.layout(left,top,right,botttom);

                //度量完一个，重置curL
                curL = right + mHorizontalSpacing;
            }

            //一行度量完，重置curL和curT
            curL = getPaddingLeft();
            curT = curT + lineHeight  + mVerticalSpacing ;
        }
    }
}
